// pages/movies/more-movie/more-movie.js
//引入全局js
var app = getApp()
var util = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    movies: {},
    navigateTitle: "",
    requestUrl: "",
    totalCount: 0,
    isEmpty: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //获取携带过来的参数navigation并将参数绑定到data中
    var category = options.category;
    this.data.navigateTitle = category;
    //根据category来判断是点击的哪一类型的页面--从而拼装对应的url重新访问数据获取更多
    var dataUrl = "";
    switch (category) {
      case "正在热映":
        dataUrl = app.globalData.doubanBase + "/v2/movie/in_theaters";
        break;
      case "即将上映":
        dataUrl = dataUrl = app.globalData.doubanBase + "/v2/movie/coming_soon";
        break;
      case "top250":
        dataUrl = app.globalData.doubanBase + "/v2/movie/top250";
        break;
    };
    //封装完url地址后重新访问
    this.data.requestUrl = dataUrl;
    util.http(dataUrl, this.processDoubanData);

  },

  processDoubanData: function (moviesDouban) {
    var movies = [];
    for (var idx in moviesDouban.subjects) {
      var subject = moviesDouban.subjects[idx];
      var title = subject.title;
      if (title.length >= 6) {
        title = title.substring(0, 6) + "...";
      }
      // [1,1,1,1,1] [1,1,1,0,0]
      var temp = {
        stars: util.convertToStarsArray(subject.rating.stars),
        title: title,
        average: subject.rating.average,
        coverageUrl: subject.images.large,
        movieId: subject.id
      }
      movies.push(temp)
    }
    var totalMovies = {}

    //如果要绑定新加载的数据，那么需要同旧有的数据合并在一起
    if (!this.data.isEmpty) {
      totalMovies = this.data.movies.concat(movies);
    }
    else {
      totalMovies = movies;
      this.data.isEmpty = false;
    }
    this.setData({
      movies: totalMovies
    });

    this.data.totalCount += 20;
    wx.hideNavigationBarLoading();
    wx.stopPullDownRefresh()
  },

  onReady: function (event) {
    wx.setNavigationBarTitle({
      title: this.data.navigateTitle
    })
  },

  //上划加载更多的方法--要设置一个上划高度的样式才能触发单这个是view组件的不需要设置，固定方法onReachBottom框架调用
  onReachBottom:function(event){
    //触发这个刷新方法就是要去对应的url加载更多的数据
    //console.log(event);
    var nextUrl = this.data.requestUrl + "?start="+this.data.totalCount+"&count=20";
    util.http(nextUrl,this.processDoubanData);
    //设置导航栏加载中动画
    wx.showNavigationBarLoading();
  },

  //下拉刷新方法固定，框架调用
  onPullDownRefresh:function(event){
    //重新加载新数据这里固定加载20条从第一位开始，要设置一些状态的该表
    var refreshUrl = this.data.requestUrl + "?star=0&count=20";
    this.data.isEmpty = true;//设置标识电影为空
    //this.data.movies = {};//置空电影集合--这个不要也行因为processDoubanData函数isEmpty为false会覆盖movies
    this.data.totalCount = 0;
    //调用方法请求
    util.http(refreshUrl,this.processDoubanData);
    wx.showNavigationBarLoading();//提示正在加载
  },

  onMovieTap: function (event) {
    //这是点击弹出电影详情页面
    var movieId = event.currentTarget.dataset.movieid;
    // console.log(movieId)
    wx.navigateTo({
      url: '../movie-detail/movie-detail?id=' + movieId,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function(optins) {
    //动态设置导航标题栏
    wx.setNavigationBarTitle({
      title: this.data.navigateTitle,
    })
  },
})